package com.searchtree.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import com.searchlist.skiplist.SkipList;
import com.searchtree.binarysearchtree.BinarySearchTree;
import com.searchtree.twofourtree.TwoFourTree;

public class Main {
	private static String document = "src/text1.txt";
	private static File f = new File(document);
	private static final String method = "Binary Search Tree";

	public static void main(String[] args) {
		System.out.println("Loading document " + document + " in a " + method);

		FileExtractor fileExtractor = new FileExtractor();
		fileExtractor.extractFile(f);
		InputStreamReader in = new InputStreamReader(System.in);
		BufferedReader reader = new BufferedReader(in);
		
		System.out.println("Document loaded.");
		System.out.println("To search type 'search' and press enter, then enter your searchterm.");
		System.out.println("To search type 'stop' and press enter.");
		
		while (true) {
			try {
				String command = reader.readLine();
				if (command.equalsIgnoreCase("search")) {
					String searchString = reader.readLine();
					if (method.equals("Binary Search Tree")) {
						BinarySearchTree bTree = BinarySearchTree.getInstance();
						System.out.println(bTree.searchKey(searchString));
					} else if (method.equals("2-4 Tree")) {
						TwoFourTree twoFourTree = TwoFourTree.getInstance();
						System.out.println(twoFourTree.searchKey(searchString));
					} else if (method.equals("Skip List")) {
						SkipList sList = SkipList.getInstance();
						System.out.println(sList.searchKey(searchString));
					}
				} else if (command.equalsIgnoreCase("stop")) {
					break;
				}
				else {
					System.out.println("Command not recognized");
				}
			} catch (IOException e) {
				System.out.println("IOException: " + e.getMessage());
			}
		}
	}

	public static void useKey(String key) {
		if (method.equals("Binary Search Tree")) {
			BinarySearchTree bTree = BinarySearchTree.getInstance();
			bTree.addNode(key);
		} else if (method.equals("2-4 Tree")) {
			TwoFourTree twoFourTree = TwoFourTree.getInstance();
			twoFourTree.addNode(key);
		} else if (method.equals("Skip List")) {
			SkipList sList = SkipList.getInstance();
			sList.insert(key);
		}
	}

}
